package com.byCreation.Profile;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.byCreatetion.adabter.bookmarkAdapter;
import com.byCreation.Control.Control;
import com.byCreation.Control.SliderMenuLeft;
import com.byCreation.t_globe.CardActivity;
import com.byCreation.t_globe.CardDetailActivity;
import com.byCreation.t_globe.ContentActivity;
import com.byCreation.t_globe.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;


import Myclass.ConnectInternet;
import Parser.HttpGetJson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MyBookmarkActivity extends Activity implements OnClickListener,OnItemClickListener{

	
	// ==================ListView======================
			private ListView lstView;
			private bookmarkAdapter adapter;
			public static ArrayList<HashMap<String, Object>> MyArrList;
			private HashMap<String, Object> map = new HashMap<String, Object>();
			private ProgressDialog mProgressDialog;

			private String[] id_detail={"You are open this page 19 times. Visit Date : 8:43 PM on 6 Feb 2014, Added : 8:43 PM on 6 Feb 2014, Last Modifiled : 8:43 PM on 6 Feb 2014", "You are open this page 20 times. Visit Date : 11:43 PM on 29 Jan 2014, Added : 11:43 PM on 29 Jan 2014, Last Modifiled : 11:43 PM on 29 Jan 2014", "You are open this page 8 times. Visit Date : 5:43 AM on 27 Jan 2014, Added : 5:43 AM on 27 Jan 2014, Last Modifiled : 5:43 AM on 27 Jan 2014","You are open this page 3 times. Visit Date : 4:22 PM on 20 Jan 2014, Added : 4:22 PM on 20 Jan 2014, Last Modifiled : 4:22 PM on 20 Jan 2014" ,"You are open this page 3 times. Visit Date : 4:22 PM on 20 Jan 2014" ,"You are open this page 64 times. Visit Date : 10:22 PM on 13 Jan 2014, Added : 10:22 PM on 13 Jan 2014, Last Modifiled : 10:22 PM on 13 Jan 2014" ,"You are open this page 33 times. Visit Date : 3:22 AM on 5 Jan 2014, Added : 3:22 AM on 5 Jan 2014, Last Modifiled : 3:22 AM on 5 Jan 2014" ,"You are open this page 6 times. Visit Date : 6:22 AM on 1 Jan 2014, Added : 6:22 AM on 1 Jan 2014, Last Modifiled : 6:22 AM on 1 Jan 2014" };
			private String[] id_title={"Khao Lak -- 25 Years of Friendship","Transfer from Phuket Airport to Khao Lak", "Khao Lak Center in Khao Lak, Phang Nga","Health Services in Ban Bang Niang" ,"A History of Khao Lak -- 25 Years of Friendship" ,"Dive Schools and Dive Shops in Khao Lak Center" ,"Health Services and Massage in Khao Lak Center" ,"Money Services in Khao Lak Center" };
	
			// ===============GoogleAnalytics==============
			private GoogleAnalytics mGaInstance;
			private Tracker mGaTracker;
			// =============================================
// ===================================googleAnalytics=============================
		@Override
		public void onStart() {
			super.onStart();
			// The rest of your onStart() code.
			EasyTracker.getInstance().activityStart(this); // Add this method.
			mGaTracker.sendView("MyBookmarkScreen");

		}

		@Override
		public void onStop() {
			super.onStop();
			// The rest of your onStop() code.
			EasyTracker.getInstance().activityStop(this); // Add this method.

		}

		// ================================================================================
			
			
			
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_my_bookmark);
		
		
		
		// =============================GoogleAnalytics=========================
		mGaInstance = GoogleAnalytics.getInstance(this);
		mGaTracker = mGaInstance.getTracker(Control.IDGoogleanalytics);

		
		 final ImageView img_close = (ImageView) findViewById(R.id.img_close);
		 img_close.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click   
            	 finish();
               
             }
         });
		
		 
		 
			SetListView();
		 
	}

	private void SetListView()
 {
		
		
		
		
 	
			lstView = (ListView)findViewById(R.id.list_view);
			this.setProgressBarIndeterminateVisibility(true);
			lstView.setOnItemClickListener(this);
			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	        mProgressDialog.setCancelable(false);  

		
	
			MyArrList = new ArrayList<HashMap<String, Object>>();

		
	  if(new ConnectInternet(MyBookmarkActivity.this).checkInternetConnection())
		new LoadData().execute();
		
 }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		// TODO Auto-generated method stub
		if(MyArrList.get(position).get("type").toString().equals("Page"))
		{
			
			Control.page_id=MyArrList.get(position).get("pageid").toString();
			SliderMenuLeft.Content_title=MyArrList.get(position).get("title").toString();
			SliderMenuLeft.Mainmenu_List_Menu_Title.add(MyArrList.get(position).get("title").toString());
			
			
			startActivity(new Intent(getBaseContext(),ContentActivity.class));
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
		else
		{
			Control.carddetail_id=MyArrList.get(position).get("pageid").toString();
			Control.carddetail_name=MyArrList.get(position).get("title").toString();
			
			
			startActivity(new Intent(getBaseContext(),CardDetailActivity.class));
			overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
		}
	
			
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public static 	int index_num=0;
	class LoadData extends AsyncTask<Object, Integer, Object> {
		boolean Cancelled=false;

		
		
		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub]
			Cancelled=true;
			super.onCancelled();
		}
		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			
			
		
//				 for (int i = 0; i <id_title.length; i++)
//				 { 
//					 map = new HashMap<String, Object>();
//					 map.put("title",id_title[i]);
//					 map.put("detail",id_detail[i]);
//					 map.put("img","");
//					 
//				     MyArrList.add(map);
//				 }
		
				 
				 
				 
					
		   try {
						
				JSONObject jsonObj=null;
				jsonObj = new JSONObject(HttpGetJson.GetJson(Control.getAPI_BookmarkList(Control.UserName),Control.user,Control.pass));
				JSONArray jsonarray_data=jsonObj.getJSONArray("list_bookmark_page");
					
			
				
				 for (int i = 0; i <jsonarray_data.length(); i++)
				 { 
					 
					 map = new HashMap<String, Object>();
					 JSONObject jsonObj_data=jsonarray_data.getJSONObject(i);

					 map.put("pageid",jsonObj_data.getString("pageid"));
					 map.put("title",jsonObj_data.getString("name"));
					 map.put("detail",(id_detail[i%4]));
					 map.put("img",jsonObj_data.getString("images"));
					 map.put("type","Page");
					 index_num=i+1;
				     MyArrList.add(map);
				 }
				
				 
				 JSONArray jsonarray_data2=jsonObj.getJSONArray("list_bookmark_card");
					
					
				 for (int i = 0; i <jsonarray_data2.length(); i++)
				 { 
					 
					 map = new HashMap<String, Object>();
					 
					 JSONObject jsonObj_data=jsonarray_data2.getJSONObject(i);
					 map.put("pageid",jsonObj_data.getString("cardid"));
					 map.put("title",jsonObj_data.getString("name"));
					 map.put("detail",(id_detail[i%4]));
					 map.put("img",jsonObj_data.getString("images"));
					 map.put("type","Card");
					 
					 
				     MyArrList.add(map);
				 }
				
				 
				 
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				 
			
				 
			return null;
		}

		@Override
		protected void onPreExecute() {

			
			adapter=null;
			
			if (adapter == null) {
				adapter = new bookmarkAdapter(MyBookmarkActivity .this, MyArrList);
				lstView.setAdapter(adapter);
				
				Log.e("TG", "adapter = new NotiListAdapter(getActivity(), MyArrList);");
				
			}

			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");
			
			
		}

		@Override
		protected void onPostExecute(Object result) {
		
			
			if(!Cancelled)
			{
				runOnUiThread (new Thread(new Runnable() { 
		         public void run() {
		                adapter.notifyDataSetChanged();      
		                

						Log.e("TG", " adapter.notifyDataSetChanged();  ");
		         }
				}));
			}
			
		}

	}


	

}
